package com.example.testcomposeproject.module.snapshotFlow

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.navigation.compose.rememberNavController
import com.example.testcomposeproject.route.AppNavHost
import com.example.testcomposeproject.ui.theme.AppTheme
import dagger.hilt.android.AndroidEntryPoint
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.material3.BottomSheetScaffold
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.SheetState
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.rememberBottomSheetScaffoldState
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.delay

@AndroidEntryPoint
class SnapShotFlowActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            AppTheme {
                SnapshotFlowVsLaunchedEffectExample()
            }
        }
    }
}


@Composable
fun SnapshotFlowVsLaunchedEffectExample() {
    var counter by remember { mutableStateOf(0) }
    var snapshotFlowCounter by remember { mutableStateOf(0) }
    var launchedEffectCounter by remember { mutableStateOf(0) }

    // 模拟计数器每秒递增
    LaunchedEffect(Unit) {
        while (true) {
            delay(1000)
            counter++
        }
    }

    Column(
        modifier = Modifier.fillMaxSize(),
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center
    ) {
        Text("Counter: $counter")

        Spacer(modifier = Modifier.height(16.dp))

        // 使用 snapshotFlow 监听 counter 的变化
        CodeWithSnapshotFlow(counter) {
            snapshotFlowCounter++
            println("snapshotFlow restarted $snapshotFlowCounter times")
        }

        Spacer(modifier = Modifier.height(16.dp))

        // 使用 LaunchedEffect 监听 counter 的变化
        CodeWithLaunchedEffect(counter, launchedEffectCounter) {
            launchedEffectCounter++
            println("LaunchedEffect restarted $launchedEffectCounter times")
        }
    }
}

@Composable
fun CodeWithSnapshotFlow(counter: Int, onLaunchedEffectRestart: () -> Unit) {
    LaunchedEffect(Unit) {
        snapshotFlow { counter }
            .collect { value ->
                onLaunchedEffectRestart()
                println("snapshotFlow: Counter value is $value")
            }
    }
}

@Composable
fun CodeWithLaunchedEffect(counter: Int, launchedEffectCounter: Int, onLaunchedEffectRestart: () -> Unit) {
    LaunchedEffect(counter) {
        onLaunchedEffectRestart()
        println("LaunchedEffect: Counter value is $counter, LaunchedEffect restarted $launchedEffectCounter times")
    }
}

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    SnapshotFlowVsLaunchedEffectExample()
}